package main

import (
	"flag"
	"fmt"
	"fssh/client"
	"os"
	"strings"

	"github.com/howeyc/gopass"
)

var version = "fssh v0.1"
var quit = false

func main() {
	host := flag.String("h", "localhost", "host name")
	username := flag.String("u", "root", "ssh username")
	password := flag.String("p", "", "ssh password")
	port := flag.Int("P", 22, "ssh port")
	alias := flag.String("a", "", "host alias")
	v := flag.Bool("v", false, "show version")
	flag.Parse()

	if *v {
		fmt.Println(version)
		return
	}

	if len(os.Args) > 1 {
		cmd := os.Args[1]
		if strings.Index(cmd, "-") == 0 {
			if len(*host) == 0 {
				panic("host not empty")
			}
			if *password == "" {
				fmt.Printf("Enter password: ")
				pass, err := gopass.GetPasswd()
				if err != nil {
					panic(err)

				}
				*password = string(pass)

			}
			if len(*alias) == 0 {
				*alias = *host
			}
			config := client.Config{
				Username: *username,
				Host:     *host,
				Password: *password,
				Port:     *port,
				Alias:    *alias,
			}
			err := client.Add(*alias, config)
			if err != nil {
				panic(err)
			}
			config.Connect()
		} else {

			cfg, err := client.Find(cmd)
			if err != nil {
				fmt.Println(err)
				return
			}
			cfg.Connect()
		}
	} else {
		fmt.Println("show help")
	}

}
